package com.sxfq.loansupermarket.backsystem.modular.basicData.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.constant.tips.Tip;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.PointRewordIconinfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.PointRewordInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchPointRewordInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.TaskScoreService;
import com.sxfq.loansupermarket.backsystem.modular.doctorLooked.entity.DaulInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/taskScore")
public class TaskScoreController extends BaseController {
    @Autowired
    TaskScoreService taskScoreService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SqlMapper sqlMapper;

    private String PREFIX = "/basicData/taskScore/";

    /**
     * 跳转到任务积分管理
     */
    @RequestMapping("")
    public String taskScore(Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "taskScore.html";
    }

    /**
     * 商品管理列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object testrInfoList(SearchPointRewordInfo info, HttpServletRequest request) {
        Page<PointRewordInfo> page = new PageFactory<PointRewordInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<PointRewordInfo> list = taskScoreService.selectPageListByQueryGetMap(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = taskScoreService.selectPageListByQueryGetMap(page, info);
            }
            page.setTotal(taskScoreService.getQueryPagesPointCount(page, info));
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }


    /**
     * 跳转到添加页面
     */
    @RequestMapping(value = "/taskScore_add")
    public String pointRewordAdd(Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "taskScore_add.html";
    }

    /**
     * 保存添加
     */
    @RequestMapping(value = "/taskScore_addOk")
    @ResponseBody
    public int taskScore_addOk(PointRewordInfo info,BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql="insert into get_points_settings_info(type_name,points) " +
                "values('"+info.getTypeName()+"','"+info.getPoints()+"')";
        return sqlMapper.insert(sql);
    }

    /**
     * 跳转修改页面
     */
    @RequestMapping(value = "/taskScore_update/{id}")
    public String taskScore_update(@PathVariable String id, Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        String sqlStr ="select * from get_points_settings_info " +
                "where type_num='"+id+"' ";
        PointRewordInfo info = sqlMapper.selectOne(sqlStr,PointRewordInfo.class);
        model.addAttribute("info", info);
        model.addAttribute("id", id);
        return PREFIX + "taskScore_edit.html";
    }

    /**
     * 保存修改
     */
    @RequestMapping(value = "/taskScore_updateOk")
    @ResponseBody
    public int taskScore_updateOk(PointRewordInfo info,BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql="update get_points_settings_info " +
                "set type_name='"+info.getTypeName()+"',points='"+info.getPoints()+"' " +
                "where type_num='"+info.getId()+"'";
        return sqlMapper.insert(sql);
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete/{id}")
    @ResponseBody
    public Integer delete(@PathVariable String id) throws Exception {
        String sqlStr ="delete from get_points_settings_info " +
                "where type_num='"+id+"' ";
        return sqlMapper.delete(sqlStr);
    }
}